﻿package cn.poco.pepsi.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.poco.pepsi.R;
import cn.poco.pepsi.bean.City;



public class SwitchCityListAdapter extends BaseAdapter{

	/** constants
	 * 
	 *   本类是用于装载切换城市的城市信息的
	 *  
	 *  
	 *  **/
	private final static String TAG = "SwitchCityListAdapter";
	private final static int FIRST_ITEM = 0;
	
	//城市市类   集合 用于保存城市对象
	private List<City> listCity;
	@SuppressWarnings("unused")
	private Context context;
	private LayoutInflater myInfater;//用来加载一个布局文件XML
	
	//是否全部城市？
	private Boolean allCity;


	public String[] cityInfo;//保存回显信息（城市名和ID）
	
	//构造函数
	public SwitchCityListAdapter(Context context,List<City> listCity,String []gpsInfo,Boolean allCity){
		
		this.context = context;
		this.listCity = listCity;
		this.cityInfo = gpsInfo;
		this.allCity = allCity;
		this.myInfater = LayoutInflater.from(context);
		
	}

	@Override
	public int getCount() {
		//ListView会先调用getCunt方法得到listview长度后再调用getView()绘制每一行
		if(allCity){
			return listCity.size()+1;
		}else{
			return 6;   //如果城市太多，就设置显示6条信息
		}
		
	}

	@Override
	public Object getItem(int position) {
		return listCity.get(position);
	}

	@Override
	public long getItemId(int position) {
		if(position==FIRST_ITEM){
			if(cityInfo.length>=2){
				return Long.parseLong(cityInfo[1]);
			}else{
				return -1;
			}
		}else{
			return Long.parseLong(listCity.get(position-1).getKey());
		}
		
	}

	
	
	//调用getView()绘制每一行*****  最重要的方法（填数据）
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(position ==FIRST_ITEM){	
			String showText = cityInfo[0];
			convertView = myInfater.inflate(R.layout.switch_city_head, null);//切换城市 列表   头
			
			
			ViewHolderHead viewHead = new ViewHolderHead();
			viewHead.title = (TextView)convertView.findViewById(R.id.gpscity);
			viewHead.title.setPadding(10, 0, 0, 0);//设置title的边距
			
			if(!showText.endsWith("-1")){
				viewHead.title.setText(showText);//  showText="GPS定位的城市"
			}else{
				viewHead.title.setText("无法定位该城市");
			}
			return convertView;
		}else{
			convertView = myInfater.inflate(R.layout.switch_city_item, null);
			ViewHolderItem viewItem = new ViewHolderItem();
			viewItem.content = (TextView)convertView.findViewById(R.id.switchcity);
			viewItem.content.setText(listCity.get(position-1).getName());
			return convertView;
		}
	}
	
	
	
	
	//内部类－－－－－－－－－－－－
	public final class ViewHolderHead{
		 public TextView title;
		}
	
	public final class ViewHolderItem{
		 public TextView content;
		}
    //－－－－－－－－－－－－
	
	public Boolean getAllCity() {
		return allCity;
	}

	public void setAllCity(Boolean allCity) {
		this.allCity = allCity;
	}
	
	

}
